{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-right">
        <button type="submit" form="form-translation" data-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fas fa-save"></i></button>
        <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-light"><i class="fas fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    {% if error_warning %}
      <div class="alert alert-danger alert-dismissible"><i class="fas fa-exclamation-circle"></i> {{ error_warning }}
        <button type="button" class="close" data-dismiss="alert">&times;</button>
      </div>
    {% endif %}
    <div class="card">
      <div class="card-header"><i class="fas fa-pencil-alt"></i> {{ text_form }}</div>
      <div class="card-body">
        <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-translation">
          <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="input-store">{{ entry_store }}</label>
            <div class="col-sm-10">
              <select name="store_id" id="input-store" class="form-control">
                <option value="0">{{ text_default }}</option>
                {% for store in stores %}
                  {% if store.store_id == store_id %}
                    <option value="{{ store.store_id }}" selected="selected">{{ store.name }}</option>
                  {% else %}
                    <option value="{{ store.store_id }}">{{ store.name }}</option>
                  {% endif %}
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="input-language">{{ entry_language }}</label>
            <div class="col-sm-10">
              <select name="language_id" id="input-language" class="form-control">
                {% for language in languages %}
                  {% if language.language_id == language_id %}
                    <option value="{{ language.language_id }}" selected="selected">{{ language.name }}</option>
                  {% else %}
                    <option value="{{ language.language_id }}">{{ language.name }}</option>
                  {% endif %}
                {% endfor %}
              </select>
            </div>
          </div>
          <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="input-route">{{ entry_route }}</label>
            <div class="col-sm-10">
              <select name="route" id="input-route" class="form-control"></select>
            </div>
          </div>
          <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="input-key">{{ entry_key }}</label>
            <div class="col-sm-10">
              <select id="input-key" class="form-control"></select><input type="text" name="key" value="{{ key }}" placeholder="{{ entry_key }}" class="form-control"/>
              {% if error_key %}
                <div class="invalid-tooltip">{{ error_key }}</div>
              {% endif %}
            </div>
          </div>
          <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="input-default">{{ entry_default }}</label>
            <div class="col-sm-10">
              <textarea name="default" placeholder="{{ entry_default }}" rows="5" id="input-default" class="form-control" disabled="disabled">{% if default %}{{ default }}{% endif %}</textarea>
            </div>
          </div>
          <div class="form-group row">
            <label class="col-sm-2 col-form-label" for="input-value">{{ entry_value }}</label>
            <div class="col-sm-10">
              <textarea name="value" rows="5" placeholder="{{ entry_value }}" id="input-value" class="form-control">{{ value }}</textarea>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('select[name="language_id"]').on('change', function() {
	$.ajax({
		url: 'index.php?route=design/translation/path&user_token={{ user_token }}&language_id=' + $('select[name="language_id"]').val(),
		dataType: 'json',
		beforeSend: function() {
			$('select[name=\'language_id\']').prop('disabled', true);
			$('select[name=\'route\']').prop('disabled', true);
			$('#input-key').prop('disabled', true);
		},
		complete: function() {
			$('select[name="\'language_id\']').prop('disabled', false);
			$('select[name=\'route\']').prop('disabled', false);
			$('#input-key').prop('disabled', false);
		},
		success: function(json) {
			html = '';

			if (json) {
				for (i = 0; i < json.length; i++) {
					if (json[i] == '{{ route }}') {
						html += '<option value="' + json[i] + '" selected="selected">' + json[i] + '</option>';
					} else {
						html += '<option value="' + json[i] + '">' + json[i] + '</option>';
					}
				}
			}

			$('select[name="route"]').html(html);

			$('select[name="route"]').trigger('change');
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

$('select[name=\'route\']').on('change', function() {
	$.ajax({
		url: 'index.php?route=design/translation/translation&user_token={{ user_token }}&language_id=' + $('select[name="language_id"]').val() + '&path=' + $('select[name="route"]').val(),
		dataType: 'json',
		beforeSend: function() {
			$('select[name=\'language_id\']').prop('disabled', true);
			$('select[name=\'route\']').prop('disabled', true);
			$('#input-key').prop('disabled', true);
		},
		complete: function() {
			$('select[name=\'language_id\']').prop('disabled', false);
			$('select[name=\'route\']').prop('disabled', false);
			$('#input-key').prop('disabled', false);
		},
		success: function(json) {
			translation = [];

			html = '<option value=""></option>';

			if (json) {
				for (i = 0; i < json.length; i++) {
					if (json[i]['key'] == $('input[name=\'key\']').val()) {
						html += '<option value="' + json[i]['key'] + '" selected="selected">' + json[i]['key'] + '</option>';
					} else {
						html += '<option value="' + json[i]['key'] + '">' + json[i]['key'] + '</option>';
					}

					translation[json[i]['key']] = json[i]['value'];
				}
			}

			$('#input-key').html(html);

			$('#input-key').trigger('change');
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

$('#input-key').on('change', function() {
	if (translation[$('#input-key').val()]) {
		$('textarea[name="default"]').val(translation[$('#input-key').val()]);

		$('input[name="key"]').val($('#input-key').val());
	} else {
		$('textarea[name="default"]').val('');
	}
});

$('select[name="language_id"]').trigger('change');
//--></script>
{{ footer }}